
// 防抖实现
const debounce = (func, wait, immediate) => {
  let timeout, result;
  let debounced = function() {
    const context = this;
    const args = arguments;
    timeout && clearTimeout(timeout);
    if (immediate) {
      const isInit = !timeout;;
      timeout = setTimeout(() => {
        func.apply(context, args);
      }, wait);
      if (isInit) {
        result = func.apply(context, args);
      }
    } else {
      timeout = setTimeout(() => {
        func.apply(context, args);
      }, wait);
    }
    return result;
  }
  debounced.cancel = function () {
    clearTimeout(timeout);
    timeout = null;
  }
  return debounced;
}

function func() {
  console.log(111);
}
const newFunc = debounce(func, 1000, false);
for(let i = 0; i < 10; i++) {
  newFunc();
  // func();

}